/**

 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 *
 * 项目名：cards-web 
 * 文件名：:RsGoalPlanServiceImpl.java
 * 模块说明：	
 * 修改历史：
 * 2018年4月5日 - liyang - 创建。
 *
 */
package com.alex.insistence.rest.goal.impl;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alex.insistence.rest.common.MyExceptionCode;
import com.alex.insistence.rest.goal.GoalPlanDTO;
import com.alex.insistence.rest.goal.GoalPlanFilter;
import com.alex.insistence.rest.goal.GoalPlanQueryResult;
import com.alex.insistence.rest.goal.RsGoalPlanService;
import com.alex.insistence.rest.goal.convert.GoalPlanConvert;
import com.alex.insistence.rest.goal.convert.GoalPlanDTOConvert;
import com.alex.insistence.rest.util.ConverterUtil;
import com.alex.insistence.web.cgform.goal.plan.entity.GoalPlanEntity;
import com.alex.insistence.web.cgform.goal.plan.entity.GoalPlanStates;
import com.alex.insistence.web.cgform.goal.plan.service.GoalPlanServiceI;
import com.gomore.jeecg.common.Assert;
import com.gomore.jeecg.common.ServiceException;
import com.gomore.jeecg.jpa.query.QueryOrderDirection;
import com.gomore.jeecg.jpa.query.QueryResult;
import com.gomore.jeecg.jpa.query.SQLSubquery;

import jodd.util.StringUtil;

/**
 * @author:liyang
 * @time:2018年4月5日 下午2:58:03
 * @since:1.0
 */
public class RsGoalPlanServiceImpl implements RsGoalPlanService {

  @Autowired
  private GoalPlanServiceI goalPlanServiceI;

  @Override
  public String save(@NotNull @Valid GoalPlanDTO planDTO) throws ServiceException {

    // 状态校验
    if (!GoalPlanStates.SAVED.equals(planDTO.getStatus())) {
      throw new ServiceException(MyExceptionCode.goalPlanStateNotSaved);
    }

    // 数据库状态校验
    if (StringUtils.isNoneBlank(planDTO.getId())) {
      GoalPlanEntity goalPlanEntity = goalPlanServiceI.get(GoalPlanEntity.class, planDTO.getId());
      validate(goalPlanEntity);
    }

    // 保存或更新
    GoalPlanEntity goalPlanEntity = GoalPlanConvert.getInstance().convert(planDTO);
    goalPlanEntity.setStatus(GoalPlanStates.SAVED);
    goalPlanServiceI.saveOrUpdate(goalPlanEntity);
    return goalPlanEntity.getId();
  }

  @Override
  public String submit(@NotNull @Valid GoalPlanDTO planDTO) throws ServiceException {

    if (!GoalPlanStates.SUBMITED.equals(planDTO.getStatus())) {
      throw new ServiceException(MyExceptionCode.goalPlanStateNotSubmited);
    }

    // 单据校验
    if (StringUtil.isNotBlank(planDTO.getId())) {
      GoalPlanEntity goalPlanEntity = goalPlanServiceI.get(GoalPlanEntity.class, planDTO.getId());
      validate(goalPlanEntity);
    }

    GoalPlanEntity goalPlanEntity = GoalPlanConvert.getInstance().convert(planDTO);
    goalPlanEntity.setStatus(GoalPlanStates.SUBMITED);
    goalPlanServiceI.saveOrUpdate(goalPlanEntity);
    return goalPlanEntity.getId();
  }

  @Override
  public void deleteById(String planId) throws ServiceException {
    Assert.assertArgumentNotNull(planId, "planId");

    GoalPlanEntity goalPlanEntity = goalPlanServiceI.get(GoalPlanEntity.class, planId);
    if (null == goalPlanEntity) {
      throw new ServiceException(MyExceptionCode.goalPlanNotFound);
    }

    if (!GoalPlanStates.SAVED.equals(goalPlanEntity.getStatus())) {
      throw new ServiceException(MyExceptionCode.goalPlanStateNotSaved);
    }
    goalPlanServiceI.delete(goalPlanEntity);

  }

  private void validate(GoalPlanEntity goalPlanEntity) throws ServiceException {

    if (null == goalPlanEntity) {
      throw new ServiceException(MyExceptionCode.goalPlanNotFound);
    }

    if (!GoalPlanStates.SAVED.equals(goalPlanEntity.getStatus())) {
      throw new ServiceException(MyExceptionCode.goalPlanStateNotSaved);
    }
  }

  @Override
  public GoalPlanQueryResult query(GoalPlanFilter filter) throws ServiceException {

    SQLSubquery query = new SQLSubquery(GoalPlanEntity.class, "o");

    if (null != filter.getBeginTime()) {
      query.appendCondition("o.beginTime >= :beginTime", filter.getBeginTime());
    }

    if (null != filter.getEndTime()) {
      query.appendCondition("o.beginTime <= :endTime", filter.getEndTime());
    }

    if (StringUtils.isNotBlank(filter.getMemberIdEquals())) {
      query.appendCondition("o.createBy =: memberId", filter.getMemberIdEquals());
    }

    if (null != filter.getStatesIn() && !filter.getStatesIn().isEmpty()) {
      query.appendCondition("o.status in :states", filter.getStatesIn());
    }

    // 默认按获得时间倒序排列
    query.appendOrder("o.updateDate", QueryOrderDirection.desc);
    QueryResult<GoalPlanEntity> results = goalPlanServiceI.query(query, filter.getPageNumber(),
        filter.getPageSize(), GoalPlanEntity.class);

    GoalPlanQueryResult pr = new GoalPlanQueryResult();
    pr.setPaging(results.getPaging());
    pr.setRecords(ConverterUtil.convert(results.getRecords(), GoalPlanDTOConvert.getInstance()));
    return pr;
  }

}
